<template>
  <a-modal
      v-model:visible="visible"
      :title="title"
      @ok="handleOk"
      :loading="loading"
  >
    <a-form
        ref="formRef"
        :model="formData"
        v-bind="{labelCol: { span: 8 },wrapperCol: { span: 16 }}"
    >
      <a-form-item name="roleCode" label="角色编码" :rules="[{ required: true, message: '请填写角色编码!' }]">
        <a-input v-model:value="formData.roleCode"/>
      </a-form-item>
      <a-form-item name="roleName" label="角色名称">
        <a-input v-model:value="formData.roleName"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
import { Ref, ref } from 'vue'
import { FormInstance, message } from 'ant-design-vue'
import { saveRole } from '@/api/sys-role'
import { useModalForm } from '@/modules/use-modules'

const formRef = ref<FormInstance>() as Ref<FormInstance>

const {
  formData,
  loading,
  visible,
  title,
  open,
  close
} = useModalForm({
  id: '',
  roleCode: '',
  roleName: '',
  password: ''
}, async (data: any) => {
  return {
    ...data,
    password: ''
  }
})

const emits = defineEmits(['ok'])

const handleOk = async () => {
  await formRef.value.validate()
  await saveRole(formData.value)
  message.success('操作成功')
  emits('ok')
  close()
}

defineExpose({ open })
</script>
